<template>
    <rx-dialog @handOk="submitForm" @cancel="closeWindow" order="buttom" btnalign="center">
        <a-form-model ref="form" layout="vertical" :model="form" :rules="rules">
            <a-row>
                <a-col :span="24">
                    <a-form-model-item
                        :labelCol="labelCol1" :wrapperCol="wrapperCol1"
                        :label="i18n('nodeJump.taskJump','任务跳转')"
                    >
                        {{ nodeText }} <b>--></b> {{ destNodeText }}
                    </a-form-model-item>
                </a-col>
            </a-row>
            <a-form-model-item v-if="!isEndNode" :label="i18n('nodeJump.taskHandler','任务处理人')" prop="toUser">
                <rx-input-button width="80%" v-model="form.toUser" @click="selectUsers"></rx-input-button>
            </a-form-model-item>
            <a-form-model-item :label="i18n('nodeJump.node','备注信息')">
                <a-textarea v-model="form.opinion" style="width: 100%"/>
            </a-form-model-item>
            <a-form-model-item :label="i18n('nodeJump.msgTypes','通知方式')">
                <a-checkbox-group :options="msgOptions" v-model="form.informTypes"></a-checkbox-group>
            </a-form-model-item>
        </a-form-model>
    </rx-dialog>
</template>
<script>

import {BaseFormModel, RxDialog, Dialog, Util} from '@lowcode/jpaas-common-lib';
import BpmtaskApi from "@/api/bpm/core/bpmTask";
import BpmPublicApi from '@/api/bpm/core/BpmPublicApi';

export default {
    name: "BpmNodeJump",
    mixins: [BaseFormModel],
    props: {
        nodeId: String,
        nodeText: String,
        taskId: String,
        actInstId: String,
        destNodeId: String,
        destNodeText: String,
        isEndNode: {
            type: Boolean,
            default: false
        },
        layerid: String,
        lydata: Object,
        destroy: Function
    },
    data() {
        return {
            rules: {
                toUser: [{
                    required: true,
                    message: this.i18n('nodeJump.taskHandlerTips', '请选变更的任务用户'),
                    trigger: 'change'
                }]
            },
            msgOptions: [],
            form: {
                taskId: this.taskId,
                actInstId: this.actInstId,
                destNodeId: this.destNodeId,
                destNodeText: this.destNodeText,
                opinion: "",
                toUser: "",
                informTypes: []
            }
        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmInst", key);
        },
        //初始化消息类型
        init() {
            this.form.taskId = this.taskId;
            BpmPublicApi.getMessageHandler().then(res => {
                res.forEach((val) => {
                    this.msgOptions.push({label: val.name, value: val.type})
                })
            })
        },
        //选择的任务
        selectUsers(vm) {
            var self_ = this;
            Dialog.openUserDialog({
                curVm: this, data: {single: false}, widthHeight: ['1200px', '600px']
            }, function (self, users) {
                var aryNames = [];
                var aryIds = [];
                for (var i = 0; i < users.length; i++) {
                    var user = users[i];
                    aryNames.push(user.fullName);
                    aryIds.push(user.userId);
                }
                var json = {text: aryNames.join(","), value: aryIds.join(",")};
                self_.form.toUser = JSON.stringify(json);
                vm.setVal(aryIds.join(","), aryNames.join(","));
            });
        },
        //关闭窗口
        closeWindow() {
            Util.closeWindow(this, 'cancel');
        },
        //实现任务的跳转
        submitForm(vm) {
            var self_ = this;
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return;
                }
                self_.form.msgTypes = self_.form.informTypes.join(",");
                if (!self_.isEndNode) {
                    self_.form.toUserIds = JSON.parse(self_.form.toUser).value;
                }

                self_.$confirm({
                    title: this.$ti18('rxMsa.tipsInfo', '提示信息'),
                    content: self_.i18n('nodeJump.confirmJump', '确认任务跳转?'),
                    okText: this.$ti18('rxForm.ackBtn', '确认'),
                    cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                    zIndex: 20000,
                    onOk() {
                        BpmtaskApi.jumpToNode(self_.form).then(resp => {
                            if (resp.success) {
                                Util.closeWindow(self_, "ok");
                            }
                            vm.loading = false;
                        });
                    },
                    onCancel() {
                        vm.loading = false;
                    }
                });

            });
        }
    }
}
</script>